package org.i0o.appliancemanager.ui.helper;

import java.awt.Component;
import java.awt.Font;
import java.awt.LayoutManager;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class UITools {

	public static JLabel createLabel(String text) {
		JLabel label = new JLabel();
		label.setText(text);

		return label;
	}

	public static JLabel createLabel(String text,
			JComponent parent) {
		JLabel label = new JLabel();
		label.setText(text);
		parent.add(label);
		return label;
	}

	public static JLabel createLabel(String text,
			JComponent parent,
			Object constraints) {
		JLabel label = createLabel(text);
		parent.add(label, constraints);
		return label;
	}

	/**
	 * 创建面板
	 * 
	 * @param layoutManager
	 *            布局管理器
	 * @return
	 */
	public static JPanel createPanel(LayoutManager layoutManager) {
		return createPanel(layoutManager, null, null);
	}

	public static JPanel createPanel(JComponent parent,
			Object constraints) {
		return createPanel(null, parent, constraints);
	}

	public static JPanel createPanel() {
		return createPanel(null, null, null);
	}

	/**
	 * 将面板添加到父容器，并套用格式
	 * 
	 * @param layoutManager
	 *            面板的布局管理器
	 * @param parent
	 *            父容器
	 * @param constraints
	 *            添加的格式
	 * @return
	 */
	public static JPanel createPanel(LayoutManager layoutManager,
			JComponent parent,
			Object constraints) {
		JPanel panel = new JPanel();
		if (layoutManager != null) {
			panel.setLayout(layoutManager);
		}
		if (parent != null && constraints != null) {
			parent.add(panel, constraints);
		} else if (parent != null) {
			parent.add(panel);
		}
		return panel;
	}

	/**
	 * 创建单行文本
	 * 
	 * @param string
	 *            文本内容Component
	 * @return
	 */
	public static JTextField createSingleText(String string) {
		JTextField field = new JTextField();
		field.setText(string);
		return field;
	}

	public static JTextField createSingleText(String string,
			JComponent parent) {
		JTextField field = new JTextField();
		field.setText(string);
		parent.add(field);
		return field;

	}

	public static JTextField createSingleText(String string,
			JComponent parent,
			Object constraints) {
		JTextField field = new JTextField();
		field.setText(string);
		parent.add(field, constraints);

		return field;
	}

	/**
	 * 创建密码输入框
	 * 
	 * @param text
	 *            输入框文本
	 * @param parent
	 *            父组件
	 * @param constraints
	 *            布局组件
	 * @return
	 */
	public static JPasswordField createPasswordField(String text,
			JComponent parent,
			Object constraints) {
		JPasswordField field = new JPasswordField();
		field.setText(text);
		if (parent != null && constraints != null) {
			parent.add(field, constraints);
			return field;
		}
		if (parent != null) {
			parent.add(field);
			return field;
		}
		return field;
	}

	/**
	 * 创建密码输入框
	 * 
	 * @param text
	 *            输入框文本
	 * @param parent
	 *            父组件
	 * @return
	 */
	public static JPasswordField createPasswordField(String text,
			JComponent parent) {
		return createPasswordField(text, parent, null);
	}

	/**
	 * 创建密码输入框
	 * 
	 * @param text
	 *            输入框文本
	 * @return
	 */
	public static JPasswordField createPasswordField(String text) {
		return createPasswordField(text, null);
	}

	/**
	 * 创建一个按钮
	 * 
	 * @param text
	 *            按钮文本
	 * @param parent
	 *            父容器
	 * @param c
	 *            布局方式
	 * @return
	 */
	public static JButton createButton(String text,
			JComponent parent,
			Object c) {
		JButton btn = new JButton();
		btn.setText(text);

		if (parent != null && c != null) {
			parent.add(btn, c);
			return btn;
		}
		if (parent != null) {
			parent.add(btn);
			return btn;
		}
		return btn;
	}

	/**
	 * 创建按钮
	 * 
	 * @param text
	 *            按钮文本
	 * @param parent
	 *            父容器
	 * @return
	 */
	public static JButton createButton(String text,
			JComponent parent) {
		return createButton(text, parent, null);
	}
	
	public static JButton createButton(String text) {
		return createButton(text,null);
	}

	public static void showErrorMessageBox(String content,
			Component component) {
		showErrorMessageBox("错误提示", content, component);
	}

	public static void showErrorMessageBox(String title,
			String content,
			Component component) {
		showMessageBox(title, content, null, JOptionPane.ERROR_MESSAGE);
	}

	public static void showMessageBox(String title,
			String content,
			Component component,
			int messageType) {
		JOptionPane.showMessageDialog(component, content, title, messageType);
	}

	public static void setFont(JComponent component,
			int size) {
		setFont(component, 0, size);

	}

	public static void setFont(JComponent component,
			int style,
			int size) {
		setFont(component, null, style, size);

	}

	public static void setFont(JComponent component,
			String family,
			int style,
			int size) {
		Font font = component.getFont();
		if (family == null) {
			family = font.getFamily();
		}
		if (style == 0) {
			style = font.getStyle();
		}
		if (size == 0) {
			size = font.getSize();
		}
		component.setFont(new Font(family, style, size));
	}

	public static void setFont(Component component,
			int size) {
		setFont(component, 0, size);
	}

	public static void setFont(Component component,
			int style,
			int size) {
		setFont(component, null, style, size);

	}

	public static void setFont(Component component,
			String family,
			int style,
			int size) {
		Font font = component.getFont();
		if (family == null) {
			family = font.getFamily();
		}
		if (style == 0) {
			style = font.getStyle();
		}
		if (size == 0) {
			size = font.getSize();
		}
		component.setFont(new Font(family, style, size));
	}

	public static void setFontBold(Component component) {
		Font font = component.getFont();
		String family = font.getFamily();
		int size = font.getSize();
		component.setFont(new Font(family, Font.BOLD, size));
	}

	 

}
